﻿#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QSettings>
#include <QThread>
#include "IPSetDialog.h"
#include "link/ModBusTcpWorker.h"
#include "link/UDPWorker.h"
#include "ProcessMonitor.h"
#include "ParentLinkDialog.h"
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class FunctionForm;

class MainWindow : public QMainWindow
{
    Q_OBJECT

signals:
    void sig_SetProNameDevType(QString proName,int DevType);
    void sig_SetIPPort(QString address,short port);
    void sig_SetReadStartAddressAndRegisterCount(QList<int> startAddress, QList<int> registerCount,int RegisterType);
    //void sig_SetWriteStartAddressAndRegisterCount(int startAddress, int registerCount,QList<quint16> data);
    void sig_StartLoopReadRegister(bool start);

    void sig_AddWriteQueue(WriteData* data);   //增加写消息队列的数据
    //void sig_HandleWriteQueue();               //处理写消息队列的数据
    void sig_TxUDPData(QList<bool> bList);


public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();


public slots:
    void  slot_readBoolData(int MegId,QList<bool> data);  //发送解析后bool数据
    void  slot_readfloatData(int MegId,QList<float> data);  //发送解析后浮点数据
    void  slot_readIntData(int MegId,QList<int> data);  //发送解析后整形数据
    void slot_SetRunState(bool isRun,int index,bool Disconnect = false);//修改运行状态, 第二个参数是16个子界面的下标
    void slot_Reset(int index);//复位的槽函数
    void slot_GeneralReset();//总复位的槽函数

    void slot_ConnectionState(bool isConnectionState);

    //void slot_ReadState(bool isState);

    void slot_WriteDataQueueFinish();//写队列完成

    void slot_showMessage(QString Msg);//子界面传递来了信息

    void slot_openIPSetDialog(bool isOpen);

    void slot_setIpAndPort(QString Ip,quint16 port);

    void slot_ReSize(int width,int height);

    void slot_ReSetIniFile(QSettings* settings);

private slots:
    void slot_processClosed();

    void slot_RxUDPData(QList<bool> bList);

    void slot_RxUDPDataWatchDog();

    void slot_PowerRunFinishWatchDog();

    //void timeoutWatchDog();
protected:
    // 重写 keyPressEvent 方法以捕获键盘事件
    void keyPressEvent(QKeyEvent *event) override;
    /*void resizeEvent(QResizeEvent *event) override;*/

private:

   void  SendDataAfterConnection();

    //样式
    void loadStyle(QString currentStyle = "black");

    void InitSendData();

    void increaseFontSize(QWidget *widget,int increment);//字体整体调整

    QSettings* m_MainSettings;

    QSettings* m_ConifgSettings;

    Ui::MainWindow *ui;

    QString m_defaultIp;
    quint16 m_defaultPort;

    QThread* m_Thread;
    ModBusTcpWorker* m_ModBusTcpWorker;
    bool m_isConnectionState;

    QList<FunctionForm*> m_FunctionForms;

    bool bFristReadRunState;       //第一次初始化的读取运行状态记录
    QList<bool>  m_ListRunStatePre;//存上一次的运行状态

    bool bFristReadFaultState;       //第一次初始化的读取总故障状态记录
    QList<bool>  m_ListFaultStatePre;//存上一次的运行状态

    bool bFristReadCurFalState;       //第一次初始化的读取电流故障状态记录
    QList<bool>  m_ListCurFalStatePre;//存上一次的读取电流故障状态

    bool bFristReadVolFalState;       //第一次初始化的读取电压故障状态记录
    QList<bool>  m_ListVolFalStatePre;//存上一次的读取电压故障状态

    bool bFristReadTempFalState;       //第一次初始化的读取温度故障状态记录
    QList<bool>  m_ListTempFalStatePre;//存上一次的读取温度故障状态


    QList<bool>  m_ListTempReSetPre;   //存上一次的读取复位状态

    QList<bool>  m_PowerClosePre;   //存上一次关闭电源状态

    QList<bool>  m_PowerRunFinishPre;   //存电源运行完毕的状态
    // QList<bool>  m_PowerRunFinishTime;//电源运

    //QTimer  *m_Timer;
    //quint64 m_TimerCnt;//定时器的计数器
    //bool isReadState;

    int m_FontSize;//字体

    QString m_Style;//样式风格

    IPSetDialog* pcIPSetDialog;

    ProcessMonitor* monitor;
    QThread* monitor_Thread;

    QThread* m_UDPThread;
    UDPWorker* m_UDPWorker;

    QList<quint8> m_ListStation1;//工位1记录

    QList<quint8> m_ListStation2;//工位2记录

    QList<bool>  m_RxUDPDataPre;//记录上一次UDPData

    ParentLinkDialog* m_ParentLinkDialog;

    quint8   m_RxUDPCnt;

    QTimer* m_UDPRXTimer;
    QTimer* m_PowerRunFinishTimer;


};
#endif // MAINWINDOW_H
